package com.animal.controller;

import com.animal.common.Constants;
import com.animal.common.Result;
import com.animal.entity.pojo.AActivity;
import com.animal.service.AvtivityService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RequestMapping("/activity")
@Controller
public class ActivityController {

    @Autowired
    private AvtivityService avtivityService;

    /**
     * 活动列表
     * @return
     */
    @ResponseBody
    @PostMapping("/activitylist/{pageNo}/{pageSize}")
    public Result selectAllActivitylist(@RequestBody(required = false) AActivity activity,
                                        @PathVariable Integer pageNo,
                                        @PathVariable Integer pageSize){
        List<AActivity> list = avtivityService.selectAllActivity(activity);
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<AActivity> pageInfo = new PageInfo<>(list);
        return Result.success(Constants.CODE_200,"",pageInfo);
    }


    /**
     * 查询某一条的信息
     * @param id
     * @return
     */
    @ResponseBody
    @PostMapping("/queryById/{id}")
    public Result queryById(@PathVariable("id") Integer id){
        AActivity activity = avtivityService.queryById(id);
        return Result.success(Constants.CODE_200,"",activity);
    }

    /**
     * 保存活动信息
     * @param activity
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public Result save(@RequestBody AActivity activity) {
        System.out.println(activity.getActStartTime());
        int count = avtivityService.insert(activity);
        if (count>0){
            return Result.success(Constants.CODE_200,"添加成功",null);
        }else {
            return Result.error(Constants.CODE_500,"添加失败");
        }
    }

    /**
     * 修改活动描述
     * @param activity
     * @return
     */
    @ResponseBody
    @PostMapping("/updateActDescribe")
    public Result updateActDescribe(@RequestBody AActivity activity){
        int i = avtivityService.updateActDescribe(activity);
        if (i>0){
            return Result.success(Constants.CODE_200,"修改成功",null);
        }else {
            return Result.error(Constants.CODE_500,"修改失败");
        }
    }

    /**
     * 删除单个
     * @param id
     * @return
     */
    @RequestMapping(value = "/deleteById/{id}",method = RequestMethod.POST)
    @ResponseBody
    public Result deleteById(@PathVariable("id") Integer id) {
        int flag = avtivityService.deleteById(id);
        if (flag > 0) {
            return Result.success(Constants.CODE_200,"删除成功",null);
        } else {
            return Result.error(Constants.CODE_500,"删除失败");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delAll",method = RequestMethod.POST)
    public Result delAll(@RequestBody List<Integer> ids){

        for(Integer id:ids){
            avtivityService.deleteById(id);
            System.out.println(id);
        }
        return Result.success(Constants.CODE_200,"删除成功",null);
    }

}
